using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX;
using TgcViewer.Example;
using TgcViewer;
using Microsoft.DirectX.Direct3D;

namespace AlumnoEjemplos.TP3D.Collider
{
    public class CollisionSphere : CollisionSurface
    {
        protected float _radio;

        public virtual float Radio {
            set {
                this._radio = value;
            }
            get {
                return this._radio;
            }
        }

        public override void Render()
        {
            Device d3dDevice = GuiController.Instance.D3dDevice;


        }

        public new Vector3 Position
        {
            get { return this.Surrounded.Position - new Vector3(0, 30, 0); }
            set { this.Surrounded.Position = value; }
        }

        public float DistanceTo(Vector3 vertex) {
            return (this.Position - vertex).Length();
            //return new Vector3(Math.Abs(this.Position.X) - Math.Abs(vertex.X), Math.Abs(this.Position.Y) - Math.Abs(vertex.Y), Math.Abs(this.Position.Z) - Math.Abs(vertex.Z)).Length();
        }
        public CollisionSphere(Placeable placeable) : base(placeable)
        {
        }

        public override bool Collides(CollisionSurface surface)
        {
            return surface.IntersectWithSphere(this);
        }
        public override bool IntersectWithBox(CollisionBox box)
        {
            float minDistance = this.Radio, currentDistance;

            foreach (Vector3 vertex in box.Vertexes) {
                currentDistance = this.DistanceTo(vertex);
                if (currentDistance < minDistance) {
                    minDistance = currentDistance;
                }
            }

            return minDistance < this.Radio;
        }
        public override bool IntersectWithSphere(CollisionSphere sphere) {
            if (sphere == this) return false;
            float distance = this.DistanceTo(sphere.Position);
            if (distance < this.Radio || distance < sphere.Radio) return true;
            return false;
        }
    }
}
